package fun.lifepoem.demo.filter;

import fun.lifepoem.demo.common.SessionManager;
import fun.lifepoem.demo.common.UserThreadLocal;
import fun.lifepoem.demo.domain.UserInfo;
import org.springframework.web.servlet.AsyncHandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;

/**
 * @author Yiwyn
 * @create 2022/12/4 1:09
 */
public class HeaderFilter implements AsyncHandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String username = request.getHeader("username");
        System.out.println("获取到的用户名" + username);
        System.out.println("拦截");

        SessionManager.set(new UserInfo() {{
            setUsername(username);
            setRoles(Collections.singletonList(username));
        }});


        return true;
    }
}
